﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace LANBoardGameHelper
{    
    public class LANBoardGameHelper : ILANBoardGameHelper
    {
        public int RollSixSidedDie(int sessionID)
        {
            var RandomInt = new System.Random();
            var rand = RandomInt.Next(1, 6);

            var myLock = new object();
            lock (myLock)
            {
                List<int> histRoll;
                if (!historicalRolls.ContainsKey(sessionID))
                {
                    historicalRolls.Add(sessionID, new List<int>());
                }
                
                histRoll = historicalRolls[sessionID];

                histRoll.Add(rand);

            }

            return rand;
        }

        //Key:sessionID Value:{Key:PlayerID Value:PlayerResources}
        private static Dictionary<int, Dictionary<int, PlayerResources>> playerResources = new Dictionary<int, Dictionary<int, PlayerResources>>();

        //Resource types: Wood, Wheat, Brick, Ore, Sheep
        public string GetCatanPlayerResources(int sessionID, int playerID)
        {
            if ((sessionID < 0) || (playerID < 0) || (!playerResources.ContainsKey(sessionID)))
            {
                return string.Empty;
            }
            
            var playersSession = playerResources[sessionID];
            if (!playersSession.ContainsKey(playerID))
            {
                return string.Empty;
            }

            var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            return oSerializer.Serialize(playersSession[playerID]);
        }

        public string GetAllCatanPlayerResources(int sessionID)
        {
            if ((sessionID < 0) || (!playerResources.ContainsKey(sessionID)))
            {
                return string.Empty;
            }

            var playersSession = playerResources[sessionID];            

            var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            return oSerializer.Serialize(playersSession.ToList());
        }

        private class PlayerResources
        {
            public int Brick { get; set; }
            public int Ore { get; set; }
            public int Sheep { get; set; }
            public int Wheat { get; set; }
            public int Wood { get; set; }
        }

        public int[] GetHistoricalRolls(int sessionID, int lastRollRetrieved)
        {
            var myLock = new object();
            lock (myLock)
            {
                if (!historicalRolls.ContainsKey(sessionID))
                {
                    return new int[]{-1};
                }
                else
                {
                    if (lastRollRetrieved < 0)
                    {
                        return new int[] {-1};
                    }
                }

                var subList = historicalRolls[sessionID].Skip(lastRollRetrieved);
                return subList.ToArray();
            }
        }

        private static List<int> sessionIDs = new List<int>();
        private static Dictionary<int, List<int>> historicalRolls = new Dictionary<int, List<int>>();

        public int CreateSessionID()
        {
            var myLock = new object();
            lock(myLock)
            {
            var sessionID = (sessionIDs.Last()) + 1;
            sessionIDs.Add(sessionID);
            return sessionID;
            }
        }
    }
}
